
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#include "GeoTorus.h"

#include "OGLVisu.h"
#include <QGLWidget>

#include "CMatrix.h"

//constructor
GeoTorus::GeoTorus() : Geometry(GT_TORUS), _smallRadius(1), _bigRadius(3)
{
	_rotationMarkRadius = 4.5f;
	_translateMarkOffset = 0.5f;
	_translateMark = CVector3f(0, 0, 0);
	_translateMark.y = _bigRadius + _smallRadius + _translateMarkOffset;
}

//destructor
GeoTorus::~GeoTorus(){}

//determine if the point v is a point of the geometry object
bool GeoTorus::isGeometry(const CVector3f& v)const
{
	//calculate the world matrix of the primitive
	CMatrix t;
	t.createTranslation(_translate);

	CMatrix rX;
	rX.createRotationX(_rotate.x * 3.14f / 180.f);
	CMatrix rY;
	rY.createRotationY(_rotate.y * 3.14f / 180.f);
	CMatrix rZ;
	rZ.createRotationZ(_rotate.z * 3.14f / 180.f);

	CMatrix s;
	s.createScale(_scale);

	CMatrix transform = s * rZ * rY * rX * t;

	//get V in local coordinate of the sphere
	CVector3f localV = v * transform.inverse();

	//calculate the center of the circle
	CVector3f locVWithoutZ = localV;
	locVWithoutZ.z = 0;
	locVWithoutZ.normalize();

	CVector3f centerOuterCircle = locVWithoutZ * _bigRadius;

	//calculate the distance between localV and the center
	float distance = (centerOuterCircle - localV).norme();

	//check the distance
	const float offsetError = 0.3f;
	if(distance > _smallRadius + offsetError || distance < _smallRadius - offsetError)
		return false;

	return true;
}

//set the sphere radius
void GeoTorus::setDimensions(float smallRadius, float bigRadius)
{
	_smallRadius = smallRadius;
	_bigRadius = bigRadius;
	_rotationMarkRadius = _smallRadius + _bigRadius + _translateMarkOffset;
	_translateMark.y = _rotationMarkRadius;
}

//small radius getter
float GeoTorus::getSmallRadius()const
{
	return _smallRadius;
}

//big radius getter
float GeoTorus::getBigRadius()const
{
	return _bigRadius;
}

//pre render the object
//void GeoTorus::preRender()
//{
//	glPushMatrix();
//
//	CVector3f eye(0, 2, 0);
//	CVector3f at(0, 0, 1);
//	CVector3f up(0, 1, 0);
//
//	//calculate the world matrix of the primitive
//	/*CMatrix t;
//	t.createTranslation(_translate);
//
//	CMatrix rX;
//	rX.createRotationX(_rotate.x * 3.14f / 180.f);
//	CMatrix rY;
//	rY.createRotationY(_rotate.y * 3.14f / 180.f);
//	CMatrix rZ;
//	rZ.createRotationZ(_rotate.z * 3.14f / 180.f);
//
//	CMatrix s;
//	s.createScale(_scale);
//
//	CMatrix transform = s * rZ * rY * rX * t;*/
//	
//	CMatrix transform;
//	transform.createTransform(eye, at, up);
//	//transform.inverse();
//	double m[16];
//	transform.getOpenGLMatrix(m);
//
//	glMultMatrixd(m);
//
//	//apply material
//	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, _material.getAmbient());
//	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, _material.getDiffuse());
//	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, _material.getSpecular());
//	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, _material.getShininess());
//}

//render the object
void GeoTorus::Render()
{
	VISU.drawTorus(_smallRadius, _bigRadius);
}

//post render function
void GeoTorus::postRender()
{
	//if the sphere is selected, draw the surrounding box and the translation mark
	if(_selected)
	{
		//glTranslatef(0, _height / 2.f, 0);
		float s = _rotationMarkRadius * 2 + _translateMarkOffset;
		float w = _smallRadius * 2 + _translateMarkOffset;
		VISU.drawSelectionBox(s, s, w);
	}

	glPopMatrix();
}
